﻿var StickyTextChangedFlag = false; //it will set to true when the text in the selected sticky note changes
function StickyTextChanged() {
    StickyTextChangedFlag = true;
 }
 function StickyNoteSave(id, textArea) {
     if (StickyTextChangedFlag) {
         var postData = 'ACTIONTYPE=DESKTOP&ACTION=STICKYNOTESAVE&content=' + textArea.value + '&SID=' + id;
         var sUrl = 'AjaxController.ashx';

         $.ajax({
             type: "POST",
             url: sUrl,
             data: postData,
             success: function (rHtml) {
                 StickyTextChangedFlag = false;
             },
             error: function (xhr, textStatus, errorThrown)
             { }

         });
     }
}
function StickyNoteClose(id) {
    var postData = 'ACTIONTYPE=DESKTOP&ACTION=STICKYNOTECLOSE&SID=' + id;
    var sUrl = 'AjaxController.ashx';

    $.ajax({
        type: "POST",
        url: sUrl,
        data: postData,
        success: function (rHtml) {

        },
        error: function (xhr, textStatus, errorThrown)
        { }

    });
}
function StickyNoteAdd() {
    var postData = 'ACTIONTYPE=DESKTOP&ACTION=STICKYNOTEADD';
    var sUrl = 'AjaxController.ashx';

    $.ajax({
        type: "POST",
        url: sUrl,
        data: postData,
        success: function (rHtml) {
           
          stickyNoteLoaded = true;
            var stickyNote = $.parseJSON(rHtml);
            var script = "<script type=\"text/javascript\">"
+ "        $(function () {\n"
+ "            $(\"#StickyNote_" + stickyNote.StickyNoteID + "\").resizable({"
+ "                animate: true,"
+ "                stop: StopResizing,"
+ "                resize:StickyResizing"
+ "            });\n"
+ "            $(\"#StickyNote_" + stickyNote.StickyNoteID + "\").draggable("
+ "            {"
+ "                stop: StopDragging"
+ "            });"
+ " $('#StickyNote_" + stickyNote.StickyNoteID + " .Content').bind(\"contextmenu\", CreateStickyMenu);"
+ "        });\n"
+ " </script>";
            var stickyHtml = '<div style="left:' + stickyNote.XPosition + 'px; top: ' + stickyNote.YPosition + 'px;width: 199px; height: 187px; " class="' + stickyNote.Style + ' StickyNote ui-resizable ui-draggable" id="StickyNote_' + stickyNote.StickyNoteID + '">' +
        '<div class="StickyNoteHeader">' +
        '<a href="#"><img onclick="StickyNoteAdd()" alt="" src="../../TemplateImages/stickyNoteAddIcon.png" id="stickyNoteAddIcon"></a><span style="width: 80%;"></span><a style="float: right;" href="#"><img onclick="StickyNoteDelete(' + stickyNote.StickyNoteID + ')" alt="" src="../../TemplateImages/stickyNoteCloseIcon.png" id="stickyNoteCloseIcon"></a></div>' +
        '<div class="Content" id="StickyContent_' + stickyNote.StickyNoteID + '">' +
            '<textarea onblur="StickyNoteSave(' + stickyNote.StickyNoteID + ',this)" onchange="StickyTextChanged()" cols="20" rows="10" class="StickyNoteTextarea"></textarea></div></div>'
            $('#desktopArea').append(script+ stickyHtml);
            stickyNoteCount++;
        
        },
        error: function (xhr, textStatus, errorThrown)
        { }

    });
}

function StickyNoteDelete(id) {
   
    var c = confirm('Are you sure you want to delete this note?');
    if (c) {
        $('#StickyNote_' + id).replaceWith('');
        var postData = 'ACTIONTYPE=DESKTOP&ACTION=STICKYNOTEDELETE&SID=' + id;
        var sUrl = 'AjaxController.ashx';

        $.ajax({
            type: "POST",
            url: sUrl,
            data: postData,
            success: function (rHtml) {
                stickyNoteCount--;
                if (stickyNoteCount == 0)
                    stickyNoteLoaded = false;
            },
            error: function (xhr, textStatus, errorThrown)
            { }

        });
    }
}

function StopDragging(event, ui) {
    var postData = 'ACTIONTYPE=DESKTOP&ACTION=STICKYNOTEDRAGED&XPOS=' + ui.position.left + '&YPOS=' + ui.position.top + '&SID=' + $(this).attr('id');
    var sUrl = 'AjaxController.ashx';

    $.ajax({
        type: "POST",
        url: sUrl,
        data: postData,
        success: function (rHtml) {

        },
        error: function (xhr, textStatus, errorThrown)
        { }

    });
}
function StopResizing(event, ui) {
    var postData = 'ACTIONTYPE=DESKTOP&ACTION=STICKYNOTERESIZE&width=' + StickyWidth + '&height=' + StickyHeight + '&SID=' + $(this).attr('id');
    var sUrl = 'AjaxController.ashx';

    $.ajax({
        type: "POST",
        url: sUrl,
        data: postData,
        success: function (rHtml) {
                },
        error: function (xhr, textStatus, errorThrown)
        { }

    });
}
function StickyResizing(event, ui) {
    StickyWidth = ui.size.width; //- ui.originalSize.width
    StickyHeight = ui.size.height;//  - ui.originalSize.height;
}
function StickyNoteClicked() {
    if (stickyNoteLoaded) {
        MinimizeMaximize();
    }
    else {
        CreateStikyNote()
     }
}
function CreateStikyNote() {
    
    var postData = 'ACTIONTYPE=DESKTOP&ACTION=STICKYNOTECREATE';
    var sUrl = 'AjaxController.ashx';
    $.ajax({
        type: "POST",
        url: sUrl,
        data: postData,
        success: function (rHtml) {
            stickyNoteLoaded = true;
            var stickyNote = $.parseJSON(rHtml);
            var script = "<script type=\"text/javascript\">"
+ "        $(function () {\n"
+ "            $(\"#StickyNote_" + stickyNote.StickyNoteID + "\").resizable({"
+ "                animate: true,"
+ "                stop: StopResizing,"
+ "                resize:StickyResizing"
+ "            });\n"
+ "            $(\"#StickyNote_" + stickyNote.StickyNoteID + "\").draggable("
+ "            {"
+ "                stop: StopDragging"
+ "            });"
+ " $('#StickyNote_" + stickyNote.StickyNoteID + " .Content').bind(\"contextmenu\", CreateStickyMenu);"
+ "        });\n"
+ " </script>";
            var stickyHtml = '<div style="left:' + stickyNote.XPosition + 'px; top: ' + stickyNote.YPosition + 'px;width: 199px; height: 187px; " class="' + stickyNote.Style + ' StickyNote ui-resizable ui-draggable" id="StickyNote_' + stickyNote.StickyNoteID + '">' +
        '<div class="StickyNoteHeader">' +
        '<a href="#"><img onclick="StickyNoteAdd()" alt="" src="../../TemplateImages/stickyNoteAddIcon.png" id="stickyNoteAddIcon"></a><span style="width: 80%;"></span><a style="float: right;" href="#"><img onclick="StickyNoteDelete(' + stickyNote.StickyNoteID + ')" alt="" src="../../TemplateImages/stickyNoteCloseIcon.png" id="stickyNoteCloseIcon"></a></div>' +
        '<div class="Content" id="StickyContent_' + stickyNote.StickyNoteID + '">' +
            '<textarea onblur="StickyNoteSave(' + stickyNote.StickyNoteID + ',this)" onchange="StickyTextChanged()" cols="20" rows="10" class="StickyNoteTextarea"></textarea></div></div>'
            $('#desktopArea').append(script+ stickyHtml);
            stickyNoteCount++;
        },
        error: function (xhr, textStatus, errorThrown)
        { }

    });
}
var minMaxStatus;
function MinimizeMaximize() {
   
    if (minMaxStatus == 'min')
        minMaxStatus = 'max';

    else
        minMaxStatus = 'min';
    var postData = 'ACTIONTYPE=DESKTOP&ACTION=STICKYNOTEMINMAX&type=' + minMaxStatus;
    var sUrl = 'AjaxController.ashx';
    $.ajax({
        type: "POST",
        url: sUrl,
        data: postData,
        success: function (rHtml) {
            var notes = rHtml.split(',');
            var i = 0;
            for (i = 0; i < notes.length; i++) {
                if (minMaxStatus == 'min') {
                    $('#StickyNote_' + notes[i]).hide();
                }
                else {
                    $('#StickyNote_' + notes[i]).show();
                }
            }
        },
        error: function (xhr, textStatus, errorThrown)
        { }

    });
}


//////////////////////right click part//////////////////////////////////////////////
var windowwidth;
var windowheight;
var checkmenu;
var curruntSticky;
$(window).ready(function () {

    $('.StickyNoteMenu').hide();

    $('#mask').click(function () {
        $(this).height(0);
        $(this).width(0);
        $('.StickyNoteMenu').hide(500);
        checkmenu = 0;
        return false;
    });

    $('#mask').bind("contextmenu", function () {
        $(this).height(0);
        $(this).width(0);
        $('.StickyNoteMenu').hide(500);
        checkmenu = 0;
        return false;
    });

    $(window).resize(function () {
        if (checkmenu == 1) {
            windowwidth = $(window).width();
            windowheight = $(window).height();
            $('#mask').css({
                'height': windowheight,
                'width': windowwidth
            });
        }

    });
});
function CreateStickyMenu(e) {
    curruntSticky = $(this).attr('id');
    curruntSticky = curruntSticky.split('_')[1];
    windowwidth = $(window).width();
    windowheight = $(window).height();
    checkmenu = 1;
    $('#mask').css({
        'height': windowheight,
        'width': windowwidth
    });
    $('.StickyNoteMenu').show(500);
    $('.StickyNoteMenu').css({
        'top': e.pageY + 'px',
        'left': e.pageX + 'px'
    });

    return false;
}
function StickyNoteMinimize() {

    var postData = 'ACTIONTYPE=DESKTOP&ACTION=STICKYNOTEMIN&SID=' + curruntSticky;
    var sUrl = 'AjaxController.ashx';
    $.ajax({
        type: "POST",
        url: sUrl,
        data: postData,
        success: function (rHtml) {
            if (rHtml == 'ok')
                $('#StickyNote_' + curruntSticky).hide();
            $('#mask').trigger('click');
        },
        error: function (xhr, textStatus, errorThrown)
        { }

    });
}
function StickyNoteSelectAll() {
    var textArea = $('#StickyNote_' + curruntSticky + ' textarea')[0];
    textArea.select();
    $('#mask').trigger('click');

}
function StickyNoteChangeStyle() {

    var postData = 'ACTIONTYPE=DESKTOP&ACTION=STICKYNOTECHANGESTYLE&SID=' + curruntSticky;
    var sUrl = 'AjaxController.ashx';
    $.ajax({
        type: "POST",
        url: sUrl,
        data: postData,
        success: function (rHtml) {
            if (rHtml != 'Err') {
                $('#StickyNote_' + curruntSticky).removeClass(rHtml.split('$')[0]);
                $('#StickyNote_' + curruntSticky).addClass(rHtml.split('$')[1])
                $('#mask').trigger('click');
            }
        },
        error: function (xhr, textStatus, errorThrown)
        { }

    });
}
